<?php

/**
* ??????
* type = 1 ??
* type = 2 ????
*/
function smarttemplate_extension_get_block($blockid = 0, $ext = '') {

        global $DB;

        $blockid = intval($blockid);
        
        
        if (!$blockid) {
                return;
        }
        $block = $DB->query_first("SELECT * FROM ".TABLE_PREFIX."block
                                                WHERE blockid='$blockid'");
        if (!$block) {
                return;
        }
        //echo $_GET['test'];
        
        //$realpath = realpath("../");
	if (!empty($ext)) {
		$tpl = new smartTemplate("block_".$block['blockid']."_".$ext.".htm");
	} else {
		$tpl = new smartTemplate("block_".$block['blockid'].".htm");
	}

        $tpl->template_dir = "/data/vhosts/xunlei.com/admin/templates/block";
        
        //$tpl->temp_dir = ROOT_PATH . "/tmp";
        //eval($block[code]);

        $location = 0;
        $item_number = $block['item_number'];
        
        if($_GET['preview_date']){
                $nowdate = strtotime($_GET['preview_date']);
        }else{
                $nowdate = strtotime(date("Y-m-d"));
        }
        $vars = $DB->query("SELECT * FROM ".TABLE_PREFIX."block_detail
                            WHERE blockid='$blockid'
                            AND  start_date <= $nowdate
                            AND ($nowdate <= end_date
                            OR end_date = 0)
                            ORDER BY pinned DESC, start_date DESC, location DESC
                            LIMIT $item_number ");
        while ($var = $DB->fetch_array($vars)) {
                /*
                $cacheVars[$cacheVars['location']]['title'] = 'title' . $location;
                $cacheVars[$cacheVars['location']][url] = '';
                $cacheVars[$cacheVars['location']][brief] = '';
                $cacheVars[$cacheVars['location']][image] = '';                
                */
                //print_rr(unserialize($var['results']));
                $result_array = unserialize($var['results']);
                $cacheVars[$location] = array_merge($result_array, array('location'=>$location));
                $location++;
        }


        //print_rr($cacheVars);

        $tpl->assign("vars", $cacheVars);
	$patt = "/<a([^>]*)>/is" ;
	$results = preg_replace($patt, "<a \\1 blockid=\"$blockid\">", $tpl->result());

	// flash add blockid
	$patt_flash = "/(http\:\/\/tracker\.movie\.xunlei\.com\/click_stat_flash_data\.php([^\"\<]*))/is" ;
	$results = preg_replace($patt_flash, "\\1&blockid=".$blockid, $results);

        return $results;

}

?>